package com.example.springpethospital.entity.examination.po;

import com.example.springpethospital.common.exception.PetHospitalException;
import java.util.LinkedList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

@Data
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
public class ExaminationQuestionPO {

    /**
     * 主键, 更新时传，懒得拆param
     */
    private Integer id;

    /**
     * 病例id todo
     */
    private Integer illCaseType;

    /**
     * 考题内容
     */
    private String context;

    /**
     * 考题选项
     */
    private List<Optiaon> options;

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Optiaon {

        /**
         * 是否正确
         */
        private Boolean right;

        /**
         * 选项内容
         */
        private String option;
    }


    public void check(boolean update) {
        List<String> errorMessage = new LinkedList<>();
        if (update && id == null) {
            errorMessage.add("主键id不能为空");
        }
        if (illCaseType == null || illCaseType <= 0) {
            errorMessage.add("病例id不能为空或者小于0");
        }
        if (StringUtils.isEmpty(context)) {
            errorMessage.add("考题内容不能为空");
        }
        if (CollectionUtils.isEmpty(options)) {
            errorMessage.add("考题选项不能为空");
        } else {
            boolean anyMatch = options.stream().noneMatch(Optiaon::getRight);
            if (anyMatch) {
                errorMessage.add("必须要有正确答案");
            }
        }
        if (CollectionUtils.isNotEmpty(errorMessage)) {
            throw new PetHospitalException(String.join( "|", errorMessage));
        }
    }

}
